package services

import (
	"easyShop/core/database/mysql"
	"easyShop/model"
	"errors"
)

func GetProductList(search string, page int) mysql.Pagination {
	conn := mysql.GetConnect()
	query := conn.Model(&model.Product{}).Select("id,name,price").Where("id >= ?", 0)
	if search != "" {
		query.Where("name like ?", "%"+search+"%")
	}
	var userList []model.Product
	r := mysql.Start().Page(page).Pagination(query, userList)

	return r
}

func GetProductInfo(id uint) (model.Product, error) {
	conn := mysql.GetConnect()
	var productInfo model.Product
	conn.Model(&model.Product{}).Where("id = ?", id).Find(&productInfo)
	if productInfo.Id == 0 {
		return productInfo, errors.New("商品不存在")
	}

	return productInfo, nil
}
